package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.CollectEntity;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class LuoboTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_LuoBO;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_UTF8;// 对应网站ID
	
	public LuoboTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new LuoboParser("总排行",TOP10Global.TYPE_OTHER,"http://luo.bo/category/spoof/","mostall"));
		PARSER.add(new LuoboParser("月排行",TOP10Global.TYPE_OTHER,"http://luo.bo/category/spoof/","mostmonth"));
		PARSER.add(new LuoboParser("周排行",TOP10Global.TYPE_OTHER,"http://luo.bo/category/spoof/","mostweek"));
		
	}
	public static void main(String arg[]) throws Exception{
		Task t=new LuoboTask();
		t.loadParser();
		t.PARSER.get(2).dealText(TOP10Global.Encoding_UTF8);
	
	}
	
}
 class LuoboParser  extends TOPParserImpl{
	private String sign;
	public LuoboParser(String c, int t, String u,String sign) {
		super(c, t, u);
		this.sign=sign;
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node=null;
		node=getNodeByAttr("ul","id",sign,body);
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 0; i < as.size(); i++) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,rank++);
			System.out.println(v);
			list.add(v);
			if(i>=9)break;
		}
		return list;
	}

	public  void build1(Node node, int rank,CollectEntity v) throws Exception {
		
	}
	public CollectEntity build(Node node, int rank) throws Exception {
		CollectEntity v=TOP10Global.getCollectEntityByType(this.getType());
		v.setRank(rank);
		String name=getAttrValueByTag(node,"title");
		if(name==null||name.trim().equals("")){
			v.setName(node.getTextContent());
		}else{
			v.setName(name);
		}
		String url=getAttrValueByTag(node,"href");
		v.setLinkUrl(url);
		return v;
	}
	
}